/*
    Copyright (c) 2008-2009, Prashanta Shrestha All Rights Reserved.
    Available under GNU Lesser General Public License >= 3.0 as published by the Free Software Foundation.
    see <http://www.gnu.org/licenses/>
*/
/*-------------------------------------------------------------------------------------
  CLASS : qDojo.dijix.Toast
  ============================
  - A widget containing message   
  -------------------------------------------------------------------------------------*/
dojo.provide("qDojo.dijix.Toast");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit._Contained");
dojo.require("dijit._Container");
dojo.require("dojo.fx"); 
  
dojo.declare("qDojo.dijix.Toast", [dijit._Widget, dijit._Templated, dijit._Contained] , {

	templatePath    : dojo.moduleUrl("qDojo.dijix", "template/Toast.html"),       
	duration: 400,
	bgImg: "",      // standard background image (png)
	bgImgAlt: "",   // background image for ie6
	bgType: 0,  
	msgType:0,      

	// ===========================================================
	// Post Create Widget
	// -----------------------------------------------------------
	postCreate: function() 
	{           
	    this.inherited(arguments);                      
	    
		var style = this.domNode.style;
		style.visibility = "hidden";
		style.position = "relative";                                    
		style.display = "";		  	    
		var alt = (this.bgImgAlt.length && dojo.isIE < 7);
        var path = dojo.moduleUrl("qDojo.dijix.resources.images");
		this.bgImg    = path+"RoundedLight.png";
		this.bgImgAlt = path+"RoundedLight.gif";				
		dojo.forEach(["roundedContent","roundedTop","roundedBottom","roundedBottomDiv"],
            function(elName){
                dojo.style(this[elName],"backgroundImage", "url(" + (alt ? this.bgImgAlt : this.bgImg)  + ")");
            },
        this);  		
	},
	// ===========================================================
	// Setup the toaster
	// -----------------------------------------------------------
	_setup: function()
	{
		var viewport = dijit.getViewport();
		var mb = dojo.marginBox(this.domNode);          
		var _left = Math.floor((viewport.l + (viewport.w - mb.w)/2));                               
		var style = this.domNode.style; 		
		style.width ="300px"
		style.top = "-90px"; 
		
		// crate animations
		var node = this.domNode;
		var parent = this.getParent();
		var id=this.id;		
				
		this._animIn = dojo.fx.combine([dojo.fx.slideTo({ top:0  , node: node , duration: this.duration }) , 
		                                dojo.fadeIn({  node: node , duration: this.duration }) ]);				
		var d = function(){parent.refreshChildren(id)};				
        this._animOut = dojo.fx.combine([dojo.fx.slideTo({top:-70, node: node , duration: this.duration , onEnd: function(){d();} }) ,
                                         dojo.fadeOut({node: node , duration: this.duration })]);		                                      		
		var doo = function( _this , btt){ btt.onclick = function(){ _this._animOut.play(); }};             		             
		doo(this , this.closeNode);
		
		var doo = function( _this , btt){ node.onclick = function(){ _this._animOut.play(); }};             		             
		doo(this , this.closeNode);                                    
	},              
	// ===========================================================
	// Show toaster. Types- auto hide; click to hide 
	// -----------------------------------------------------------
	show : function(type,message,title)
	{     
	   this.msgType = type;	               
	   if(!this._alreadyInitialized){
	       this._setup();
	       this._alreadyInitialized=true;
	   }				
	   this.titleNode.innerHTML = title? title : "&nbsp;";           
	   this.messageNode.innerHTML = message;           
	   if(this.msgType==1){
	       this.closeNode.style.visibility = "visible";
	       this.closeNode.style.height     = "16px";		    
	   }else{
	       this.closeNode.style.visibility = "hidden";
	       this.closeNode.style.height     = "0px";	        
	   }
       dojo.style(this.domNode, "opacity", 0);
	   this.domNode.style.visibility="visible";
	   this.domNode.style.zIndex="1000";		
	   this._animIn.play(); 
	           
	   this.getParent().childShown();  								               
	},
	startCounter : function()
	{
	   var _this = this;
	   setTimeout(function(){_this._animOut.play();},2000);       	   
	}
});